﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.Survivor
{
    public class StatisticManager: BattleSingleton<StatisticManager>
    {
        private Dictionary<int, BattleHeroStatistic> heroStatisticMap = new Dictionary<int, BattleHeroStatistic>();

        public List<BattleHeroStatistic> GetSelfHeroList()
        {
            var list = new List<BattleHeroStatistic>();
            foreach (var pair in heroStatisticMap)
            {
                if (pair.Value.team == 0)
                {
                    list.Add(pair.Value);
                }
            }
            return list;
        }
        
        public void AddHero(BattleUnit unit)
        {
            if (heroStatisticMap.ContainsKey(unit.unitId))
            {
                return;
            }
            BattleHeroStatistic statistic = ClassManager.Get<BattleHeroStatistic>();
            statistic.team = unit.team;
            statistic.heroId = unit.heroId;
            statistic.attack = (int)unit.attack;
            statistic.hp = unit.totalHp;
            statistic.defence = (int)unit.defence;
            statistic.skillDamage = 0;
            statistic.skillTimes = 0;
            statistic.normalAttackDamage = 0;
            statistic.normalAttackTimes = 0;
            heroStatisticMap.Add(unit.unitId, statistic);
        }

        public void AddTimes(BattleUnit unit,EBulletDamageType damageType)
        {
            if (!heroStatisticMap.TryGetValue(unit.unitId, out var statistic))
            {
                return;
            }
            if (damageType == EBulletDamageType.Skill)
            {
                statistic.skillTimes++;
            }
            else
            {
                statistic.normalAttackTimes++;
            }
        }
        
        public void AddDamage(BattleUnit unit,EBulletDamageType damageType, int damage)
        {
            if (!heroStatisticMap.TryGetValue(unit.unitId, out var statistic))
            {
                return;
            }
            if (damageType == EBulletDamageType.Skill)
            {
                statistic.skillDamage += damage;
            }
            else
            {
                statistic.normalAttackDamage += damage;
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            foreach (var pair in heroStatisticMap)
            {
                ClassManager.Free(pair.Value);
            }
            heroStatisticMap.Clear();
        }
    }
}
